package com.chenyun.satoken.starter.dao.fastjson2;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.util.SaFoxUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;

/**
 * 直接复制官方的实现
 * @author:
 * @CreateTime: 2023-09-25  22:24
 * @Description:
 */
public class SaSessionForFastjson2Customized extends SaSession {
    private static final long serialVersionUID = -7600983549653130681L;

    public SaSessionForFastjson2Customized() {
    }

    public SaSessionForFastjson2Customized(String id) {
        super(id);
    }

    public <T> T getModel(String key, Class<T> cs) {
        return SaFoxUtil.isBasicType(cs) ? SaFoxUtil.getValueByType(this.get(key), cs) : JSON.parseObject(this.getString(key), cs);
    }

    public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
        Object value = this.get(key);
        if (this.valueIsNull(value)) {
            return (T)defaultValue;
        } else {
            return SaFoxUtil.isBasicType(cs) ? SaFoxUtil.getValueByType(this.get(key), cs) : JSON.parseObject(this.getString(key), cs);
        }
    }

    @JSONField(
            serialize = false
    )
    public long getTimeout() {
        return super.getTimeout();
    }
}
